<?php

/**
 * 
 * @author yuxun
 *
 */
class MessService extends Service {
	/**
	 * -1 签名失败或者参数不全
	 * 0 成功
	 * 1 找不到渠道
	 * 2 找不到服务器
	 * 3 后台渠道和服务器的配置有问题
	 * 4 入库失败
	 * 5 发送邮件失败
	 * @param unknown $ar
	 * @return multitype:number string
	 */
	public static function addMessage($ar){
		if($ar['channel'] == ''){
			$json_arr = array('errcode'=>-1,'msg'=>'need to parameter channel','data'=>'');
			return $json_arr;
		}
		
		if($ar['server'] == ''){
			$json_arr = array('errcode'=>-1,'msg'=>'need to parameter server','data'=>'');
			return $json_arr;
		}
		
		if($ar['role_id'] == ''){
			$json_arr = array('errcode'=>-1,'msg'=>'need to parameter role_id','data'=>'');
			return $json_arr;
		}
		
		if($ar['role_name'] == ''){
			$json_arr = array('errcode'=>-1,'msg'=>'need to parameter role_name','data'=>'');
			return $json_arr;
		}
		
		if($ar['role_level'] == ''){
			$json_arr = array('errcode'=>-1,'msg'=>'need to parameter role_level','data'=>'');
			return $json_arr;
		}
		
		if($ar['desc'] == ''){
			$json_arr = array('errcode'=>-1,'msg'=>'need to parameter desc','data'=>'');
			return $json_arr;
		}
		
		//根据channel获取channel_id
		$modChannel = new ModChannel();
		$channel_obj = $modChannel->findByAttributes(array('tab'=>$ar['channel']));
		if(!$channel_obj){
			$json_arr = array('errcode'=>1,'msg'=>'channel '.$ar['channel'].' is not found','data'=>'');
			return $json_arr;
		}
		$channelInfo = $channel_obj->getAttributes();
		$channel_id = $channelInfo['id'];
		
		//根据channel_id和server获取服务器id
		$modServer = new ModServer();
		$server_obj = $modServer->findAllByAttributes(array('name'=>$ar['server']));
		if(!$server_obj){
			$json_arr = array('errcode'=>2,'msg'=>'server '.$ar['server'].' is not found','data'=>'');
			return $json_arr;
		}
		
		if(count($server_obj) == 1){
			$server_id = $server_obj[0]->id;
		}else{
			$mclist = $modServer->getIDForNameChannel($ar['server'],$channel_id);
			if(count($mclist) == 0){
				$json_arr = array('errcode'=>3,'msg'=>'server_channel '.$ar['server'].' is not found','data'=>'');
				return $json_arr;
			}
			$server_id = $mclist[0]['id'];
		}
		
		//入库
		$modMessage = new ModMessage();
		$modMessage->game = 'mango';
		$modMessage->channel_id = $channel_id;
		$modMessage->server_id = $server_id;
		$modMessage->role_id = $ar['role_id'];
		$modMessage->role_name  = $ar['role_name'];
		$modMessage->role_level = $ar['role_level'];
		$modMessage->desc = $ar['desc'];
		 
		$modMessage->create_user   = 1;
		$modMessage->create_time   = time();
		$modMessage->update_user   = 1;
		$modMessage->update_time   = time();
		$result = $modMessage->save();
		if($result !== false){
			//成功之后马上回复邮件
			$params = array();
			$params['server_id'] = $server_id;
			$params['rolename'] = $ar['role_name'];
			
			$modSystem = new ModSystem();
			$map = array();
			$map['g'] = 'mango';
			$map['inc_type'] = 'message';
			$list = $modSystem->select($map);
			$m_data = array();
			foreach ($list as $key=>$val){
				$m_data[$val['name']] = $val['value'];
			}

			$message_title = $m_data['message_title'] == ''? '系统回复':$m_data['message_title'];
			$message_content = $m_data['message_content'] == ''? '感谢您的反馈！':$m_data['message_content'];
			$params['title'] = $message_title;
			$params['content'] = $message_content;
			
			$send_result = self::send($params);
			
			if($send_result['state']){
				$json_arr = array('errcode'=>0,'msg'=>'success','data'=>'');
				return $json_arr;
			}else{  //发送请求失败
				$json_arr = array('errcode'=>5,'msg'=>'send email fail','data'=>'');
				return $json_arr;
			}	
			
		}else{
			$json_arr = array('status'=>4,'msg'=>'添加入库失败','data'=>'' );
			return $json_arr;
		}	
		
	}
	
	public static function send($params){		
		$role_accessory = array();
		$role_accessory['item'][] = array(
			'id'    => $params['rolename'],
		);
		
		$params['rolename'] = json_encode($role_accessory);
		$params['roleid']       = '';
		$params['accountname']  = '';
		
		
		$item_array = array();
		$accessory['item'] = $item_array;
		
		$params['attached'] = json_encode($accessory);		
		
		$api = new ApiService($params['server_id']);
		$args = array(
				'sendtype'      => 1,
				'rolename'      => $params['rolename'],
				'roleid'        => $params['roleid'],
				'accountname'   => $params['accountname'],
				'mailtitle'     => $params['title'],
				'mailcontent'   => $params['content'],
				'attached'      => $params['attached'],
		);
			
		$result = $api->sendGift($args);
		
		return $result;
	}
	
	
	

}